var ADMIN_FILTER = {
		key_search: '',
		enabled: '-1',
		action:{
			error:{
				rowSelectedEmpty: 'Select row to action.',
				ajaxError: 'Do not updated, contact with vcqviet@gmail.com for this error.'
			},
			confirm:{
				deleteConfirm: 'Do you want to delete it?'
			}
		},
		initFilter:function(){
			$('input.number').keydown(function(event) {
				// Allow: backspace, delete, tab, escape, and enter,  Ctrl+A, home, end, left, right
				if ( event.keyCode ==190 || event.keyCode == 110 ||  event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true) || (event.keyCode >= 35 && event.keyCode <= 39)) {
					return;
					//return false;
				}
				else {
					// Ensure that it is a number and stop the keypress
					if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
						event.preventDefault(); 
					}
				}
			});
			$('#filter_enable').val(ADMIN_FILTER.enabled).attr('selected', true);
			$('#filter_key').focus(function(){
				if($(this).val() == ADMIN_FILTER.key_search){
					$(this).val('');
				}
			});
			$('#filter_key').focusout(function(){
				if($(this).val() == ''){
					$(this).val(ADMIN_FILTER.key_search);
				}
			});
			$('#filter_submit').click(function(){
				$('#action_name').val('none');
				if($('#filter_key').val()==ADMIN_FILTER.key_search){
					$('#filter_key').val('');
				}
			});
			
			$('input.select-year').each(function(){
				var $ob = $(this);
				var $from = parseInt($ob.attr('data-from'));
				var $to = parseInt($ob.attr('data-to'));
				var $val = $ob.val();
				var $sel = $('<select class="combo-box" name="select_option'+$ob.attr('id')+'" id="sel_'+$ob.attr('id')+'" data-id="'+$ob.attr('id')+'"><option value="0">'+$ob.attr('data-option-all')+'</select>');
				for( var $i = $from; $i <= $to; $i++){
					$sel.append('<option value="'+$i+'" '+($i==parseInt($ob.val())?'selected':'')+'>'+$i+'</option>')
				};
				$ob.parent().append($sel);
				
				$sel.change(function(){
					var $obi = $('#'+$(this).attr('data-id'));
					$obi.val($sel.val());
				});
			});
			$('div.tabs').tabs();
			$('.clickShow').click(function(){
				$(this).next('div').slideToggle();
			});
			ADMIN_FILTER.initList();
			
		},
		initList:function(){
			$('#select_checkall').click(function(){
				$('input.select_list').attr('checked', this.checked);
			});
			$('a.a-control').click(function(){
				if($(this).attr('data-confirm')=='confirm'){
					if(!confirm(ADMIN_FILTER.action.confirm.deleteConfirm)){
						return false;
					}
				}
				if($(this).attr('data-require')=='selected'){
					if(ADMIN_FILTER.checkSubmitAction()){
						$('#action_name').val($(this).attr('data-action'));
						$('#form_main').submit();
					}else{
						alert(ADMIN_FILTER.action.error.rowSelectedEmpty);
					}
				}else{
					$('#action_name').val($(this).attr('data-action'));
					$('#form_main').submit();
				}
				return false;
			});
			$('a.a-control-get').click(function(){
				$(this).attr('href',$('#form_main').attr('action')+'&action_name='+$(this).attr('data-action'));
				return true;
			});
			$('img.control-ajax').css('cursor','pointer').click(function(){
				var urlPost=$('#form_main').attr('action');
				var obj = $(this);
				$.ajax({
					type: "POST",
					url: urlPost,
					data: {
						val: obj.attr('data-value'),
						id: obj.attr('data-id'),
						action_name: obj.attr('data-action')
					},
					cache: false,
					success: function(data){
						var dataJson = $.parseJSON(data);
						if(parseInt(dataJson.status,10)==1){
							obj.attr('src',dataJson.data.src);
							obj.attr('data-value',dataJson.data.value);
							obj.attr('title',dataJson.data.title);
						}else{
							alert(ADMIN_FILTER.action.error.ajaxError)
						}
					}
				});
			});
		},
		checkSubmitAction:function(){
			var selected = false;
			$('input.select_list').each(function(){
				if(this.checked){
					selected = true;
				}
			});
			return selected;
		}
}